﻿using SvGame.Objects;
using UtilLib;

namespace SvGame.Logic;

public static partial class GameLogic
{
    public static List<IRole> GetAttackTargets(this IRole me)
    {
        List<IRole> targets = new();
        // 清除不在同一房间的对手
        me.CheckFight();

        foreach (var item in me.Comb.EnemyList)
        {
            if (me.Room.GetRole(item, out var role))
            {
                if (role.FightHas(me.Uid))
                {
                    targets.Add(role);
                }
            }
        }
        return targets;
    }
    public static IRole GetAttackTarget(this IRole me)
    {
        var targets = me.GetAttackTargets();
        // 没有对手
        if (targets.Count <= 0)
        {
            return null;
        }
        return targets.RandomSingle();
    }
    public static void Attack(this IRole me)
    {
        var target = GetAttackTarget(me);
        if (target == null)
        {
            return;
        }

        // 尝试攻击
        me.TryAttack(target);
    }
    public static bool CanFight(this Room room)
    {
        if (!room.Cfg.CanFight)
        {
            return false;
        }
        if (GetCity(room.Cfg.CityUid, out var city))
        {
            if (!city.Cfg.CanFight)
            {
                return false;
            }
        }
        return true;
    }

}
